home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / demoCDEF ƒ / demoCDEF.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-17  |  8.8 KB  |  312 lines  |  [TEXT/KAHL]

  1. // ---------------------------------------------------------------------------------- 
  2. // File        : demoCDEF.c
  3. // Purpose    : Demo routine for various CDEF's and dialog routines
  4. // Author    : Jim Stout
  5. // Date        : November 2, 1991
  6. // ------------------------------------------------------------------------------------
  7. #include "TogLib.h"
  8. #include "movableModal.h"
  9. #include "demoCDEF.h"
  10.  
  11. static void            setNewFont    (DialogPtr theDialog);
  12. static void            setNewSize    (DialogPtr theDialog, long newSize);
  13. static void         doDialog    (void);
  14. static pascal char filter        (DialogPtr theDialog, EventRecord *theEvent, short *theItem);
  15. extern void updateWindow(WindowPtr);
  16.  
  17. #define MAXFONTSIZES 5
  18.  
  19. // ------------------------------------------------------------------------------------
  20. // routine called by movableModalDialog
  21. // ------------------------------------------------------------------------------------
  22. extern void updateWindow(WindowPtr w)
  23. {
  24.     SetPort(w);
  25.     BeginUpdate(w);
  26.     EndUpdate(w);
  27. }
  28.  
  29. // ------------------------------------------------------------------------------------
  30. // Mainline starts here :)
  31. // ------------------------------------------------------------------------------------
  32. main()
  33. {
  34.     InitGraf(&thePort);
  35.     InitWindows();
  36.     InitFonts();
  37.     InitMenus();
  38.     TEInit();
  39.     InitCursor();
  40.     InitDialogs(0L);    
  41.         
  42.     doDialog();
  43. }
  44.  
  45. // ------------------------------------------------------------------------------------
  46. // lots of neat stuff follows…
  47. // ------------------------------------------------------------------------------------
  48. static void doDialog()
  49. {
  50.  
  51.     DialogPtr            theDialog;
  52.     short                itemHit,t,item,newSize;
  53.     long                longVal;
  54.     Rect                r;
  55.     ControlHandle        h,h2;
  56.     Str255                fontSize,s;
  57.     popUpPrivateDataH    hPpd;
  58.     FontInfo            fInfo;
  59.  
  60.     theDialog = GetNewDialog(128,0L,(DialogPtr)-1);
  61.     if(theDialog) {
  62.     
  63.         SetPort(theDialog);    
  64.         
  65.         initTogButtons(theDialog,TB1,TB4);                    // initialize TOG button stuff
  66.         
  67.         GetDItem(theDialog,SPIN2,&t,(Handle *)&h,&r);        // set SPIN2 to update EDITSPIN
  68.         longVal = MAKELONG(EDITSPIN, 10);                    // and to increment by 10's
  69.         SetCRefCon(h,longVal);                
  70.         SelIText(theDialog, EDITSPIN, 0, 32767);
  71.         
  72.         GetDItem(theDialog,FONTPOPUP,&t,(Handle *)&h,&r);    // set ctl value for popups
  73.         hPpd = (popUpPrivateDataH)(*h)->contrlData;            // and set the checkmarks
  74.         SetCtlValue(h, 1);
  75.         CheckItem((*hPpd)->mHandle, 1, checkMark);
  76.         
  77.         GetDItem(theDialog,FONTSIZE,&t,(Handle *)&h,&r);
  78.         hPpd = (popUpPrivateDataH)(*h)->contrlData;
  79.         SetCtlValue(h, 3);
  80.         CheckItem((*hPpd)->mHandle, 3, checkMark);
  81.  
  82.         ShowWindow(theDialog);
  83.         
  84.         while(itemHit != OK)
  85.         {
  86.             movableModalDialog((ProcPtr)filter,&itemHit);    // do a movable dialog!!
  87.             
  88.             switch (itemHit) {
  89.                 case RB1:
  90.                 case RB2:
  91.                 case RB3:
  92.                     setRadioButtons(theDialog,itemHit,RB1,RB3);
  93.                     resetTogButtons(theDialog,TB1);
  94.                 break;
  95.                 case TB1:
  96.                 case TB2:
  97.                 case TB3:
  98.                 case TB4:
  99.                     setTogButtons(theDialog,itemHit,TB1,TB4);
  100.                 break;
  101.                 case CB1:
  102.                 case CB2:
  103.                     GetDItem(theDialog,itemHit,  &t,(Handle *)&h,&r);
  104.                     SetCtlValue(h, !GetCtlValue(h));
  105.                     resetTogButtons(theDialog,TB1);
  106.                 break;
  107.                 case EDITSIZE:
  108.                     GetDItem(theDialog,EDITSIZE,  &t,(Handle *)&h,&r);
  109.                     GetIText((Handle)h, s);
  110.                     StringToNum(s, &longVal);
  111.                     if(longVal > 128 || longVal < 0) {
  112.                         SetIText((Handle)h, "\p12");
  113.                         SysBeep(1);
  114.                     }
  115.                     else
  116.                         setNewSize(theDialog, longVal);
  117.                 break;
  118.                 case EDITSPIN:
  119.                     GetDItem(theDialog,EDITSPIN,  &t,(Handle *)&h,&r);
  120.                     GetIText((Handle)h, s);
  121.                     StringToNum(s, &longVal);
  122.                     GetDItem(theDialog,SPIN2,      &t,(Handle *)&h,&r);
  123.                     SetCtlValue(h, (short)longVal);
  124.                 break;
  125.                 case ENABLE:
  126.                     GetDItem(theDialog,CB1,      &t,(Handle *)&h,&r);
  127.                     GetDItem(theDialog,ENABLE,&t,(Handle *)&h2,&r);
  128.                     if((*h)->contrlHilite == 0)
  129.                         SetCTitle(h2,"\pEnable\rControls");
  130.                     else
  131.                         SetCTitle(h2,"\pDisable\rControls");
  132.                         
  133.                     for(item=CB1;item<=SETBTN;item++) {
  134.                         GetDItem(theDialog,item,&t,(Handle *)&h,&r);
  135.                         if((*h)->contrlHilite == 0)
  136.                             HiliteControl(h,255);
  137.                         else
  138.                             HiliteControl(h,0);
  139.                     }
  140.                     resetTogButtons(theDialog,TB1);
  141.                 break;
  142.                 case FONTPOPUP:
  143.                     resetTogButtons(theDialog,TB1);
  144.                 break;
  145.                 case FONTSIZE:
  146.                     GetDItem(theDialog,FONTSIZE,&t,(Handle *)&h,&r);
  147.                     hPpd = (popUpPrivateDataH)(*h)->contrlData;
  148.                     
  149.                     newSize = GetCtlValue(h);
  150.                     GetItem((*hPpd)->mHandle, newSize, fontSize);
  151.                     GetDItem(theDialog,EDITSIZE,&t,(Handle *)&h,&r);
  152.                     SetIText((Handle)h, fontSize);
  153.                     
  154.                     resetTogButtons(theDialog,TB1);
  155.                 break;
  156.                 case SETBTN:
  157.                     setNewFont(theDialog);
  158.                 break;
  159.                 default:
  160.                     resetTogButtons(theDialog,TB1);
  161.                 break;
  162.             }
  163.         }
  164.         DisposDialog(theDialog);
  165.     }
  166. }
  167. //---------------------------------------------------------------------------------- 
  168. // A simple dialog filter proc - all it does is limit typing to numbers in the
  169. // EDITSPIN or EDITSIZE edit text items.
  170. //----------------------------------------------------------------------------------
  171. static  pascal char filter(DialogPtr theDialog, EventRecord *theEvent, short *theItem)
  172. {
  173.     char result = FALSE;
  174.     char c;
  175.     short itemEdit, t;
  176.     ControlHandle h;
  177.     Rect          r;
  178.     
  179.     switch(theEvent->what) {
  180.         case nullEvent:                                    // update the "progress" bar
  181.             GetDItem(theDialog, PROGBAR, &t, (Handle *)&h, &r);
  182.             if((*h)->contrlValue >= (*h)->contrlMax)
  183.                 SetCtlValue(h, 0);
  184.             else
  185.                 SetCtlValue(h, GetCtlValue(h)+1);
  186.         break;
  187.         case keyDown:
  188.          case autoKey:
  189.              itemEdit = ((DialogPeek) theDialog)->editField + 1;
  190.              
  191.              if(itemEdit == EDITSPIN || itemEdit == EDITSIZE) {
  192.                  result = TRUE;
  193.                  c = theEvent->message & charCodeMask;
  194.                  switch (c) {
  195.                      case _BACK:                            // backspace
  196.                      case _TAB:                            // tab
  197.                      case _PLUS:                            // +
  198.                      case _MINUS:                        // -
  199.                      case _LEFT:                            // left/right arrow keys
  200.                      case _RIGHT:
  201.                          result = FALSE;                    // are allowed
  202.                      break;
  203.                  }
  204.                 if(result && (c < _ZERO || c > _NINE)) {    // only allow digits
  205.                     SysBeep(1);
  206.                 }
  207.                 else
  208.                     result = FALSE;
  209.             }
  210.         break;
  211.     }
  212.     return result;
  213. }
  214.  
  215. //---------------------------------------------------------------------------------- 
  216. //    Set a new font for this dialog
  217. //----------------------------------------------------------------------------------
  218. static void setNewFont(DialogPtr theDialog)
  219. {
  220.     short                t,val,newFont;
  221.     long                newSize;
  222.     ControlHandle        h;
  223.     Rect                r;
  224.     Str255                fontName,fontSize;
  225.     FontInfo            fInfo;
  226.     popUpPrivateDataH    hPpd;
  227.     
  228. // get the font size from the popup menu
  229.  
  230.     GetDItem(theDialog,FONTSIZE,&t,(Handle *)&h,&r);
  231.     val = GetCtlValue(h);
  232.     hPpd = (popUpPrivateDataH)(*h)->contrlData;
  233.     GetItem((*hPpd)->mHandle, val, fontSize);
  234.     StringToNum(fontSize, &newSize);
  235.  
  236. // get the font from the popup menu
  237.     
  238.     GetDItem(theDialog,FONTPOPUP,&t,(Handle *)&h,&r);
  239.     val = GetCtlValue(h);
  240.     hPpd = (popUpPrivateDataH)(*h)->contrlData;
  241.     GetItem((*hPpd)->mHandle, val, fontName);
  242.     GetFNum(fontName, &newFont);
  243.  
  244. // set new font & size for current port
  245.     
  246.     TextFont(newFont);
  247.     TextSize((short)newSize);
  248.  
  249. // set new font for TEHandle in dialog (this is a crude hack!)
  250.     
  251.     GetFontInfo(&fInfo);
  252.     (*((DialogPeek)theDialog)->textH)->txFont = newFont;
  253.     (*((DialogPeek)theDialog)->textH)->txSize = (short)newSize;
  254.     (*((DialogPeek)theDialog)->textH)->fontAscent = fInfo.ascent;
  255.  
  256. // redraw the dialog in the new font & size
  257.     
  258.     DrawDialog(theDialog);
  259. }
  260.  
  261. //---------------------------------------------------------------------------------- 
  262. //    Set a new size into the popup menu for font size.  Called when the 
  263. //    EDITSIZE dialog item is changed, will add or remove a "custom" size
  264. //    and a separator line as required.
  265. //----------------------------------------------------------------------------------
  266. static void    setNewSize    (DialogPtr theDialog, long newSize)
  267. {
  268.     short                type,val,inx,max;
  269.     long                longVal;
  270.     ControlHandle        h;
  271.     MenuHandle            hMenu;
  272.     Rect                r;
  273.     popUpPrivateDataH    hPpd;
  274.     Str255                s;
  275.     Boolean                found=false;
  276.     
  277.     GetDItem(theDialog,FONTSIZE,&type,(Handle *)&h,&r);        // get menu handle
  278.     hPpd = (popUpPrivateDataH)(*h)->contrlData;
  279.     hMenu = (*hPpd)->mHandle;
  280.     
  281.     val = GetCtlValue(h);                                    // uncheck current item
  282.     CheckItem(hMenu, val, FALSE);
  283.     
  284.     max = CountMItems(hMenu);
  285.     for(inx=1;inx<=max;inx++) {                                // see if typed in value
  286.         GetItem(hMenu, inx, s);                                // matches a menu item
  287.         StringToNum(s, &longVal);
  288.         if(longVal == newSize) {                            // yep, got a match but it is
  289.             if(inx > 2 & max > MAXFONTSIZES) {                // not custom, so let's
  290.                 DelMenuItem(hMenu,1);                        // delete the custom setting
  291.                 DelMenuItem(hMenu,1);                        // and the separator line
  292.                 inx-=2;
  293.             }
  294.             CheckItem(hMenu, inx, TRUE);                    // check new item
  295.             SetCtlValue(h, inx);
  296.             found = true;
  297.             break;
  298.         }
  299.     }
  300.     if(!found) {                                            // no match, set custom item
  301.         NumToString(newSize, s);
  302.         if(max == MAXFONTSIZES) {                            // add a custom value
  303.             InsMenuItem(hMenu, "\p(-", 0);
  304.             InsMenuItem(hMenu, s, 0);
  305.         }
  306.         else {
  307.             SetItem(hMenu, 1, s);                            // replace existing custom
  308.         }
  309.         CheckItem(hMenu, 1, TRUE);
  310.         SetCtlValue(h, 1);
  311.     }
  312. }